const Service = require('../../../core/service/ApiService');
const {Transactional} = require('../../../../plugin/egg-sequelize-transactional');

class ServiceService extends Service {

    //获取所有服务项目
    async getAllServiceItem() {
        return {
            list: await this.service.mobile.service.dao.getAllServiceItem()
        };
    }

    //创建服务信息
    @Transactional()
    async startService(aged_id, status) {
        const dao = this.service.mobile.service.dao;
        const now = this.helper.YMDhms(new Date(this.ctx.starttime));
        const staff_id = this.ctx.session.svc_staff_id;
        const ps = this.helper.makePromises();
        if (status === this.constant.SVC_STATUS.SIGNED) {
            ps
                .push(dao.addSvcRecord(staff_id, aged_id, now, status))
                .push(dao.increaseCurMonthSvcCnt(staff_id, 1))
                .push(dao.updateAgedSvcStatusAndLastSvcTime(aged_id, now, this.constant.AGED_SVC_STATUS.IN_SERVICE));
        } else if (status === this.constant.SVC_STATUS.EXCEPTION) {
            ps
                .push(dao.addSvcRecord(staff_id, aged_id, now, status))
                .push(dao.increaseCurMonthSvcAbnormalCnt(staff_id, 1));
        }
        await ps.execute();
    }

    //提交服务信息
    @Transactional()
    async commitService(service) {
        const dao = this.service.mobile.service.dao;
        const record = await dao.getInServiceRecord(this.ctx.session.svc_staff_id, service.aged_id);
        if (!record)
            throw new this.error.BusinessError('没有该记录');
        const today = this.helper.farawayDays();
        if (record.start_time.getTime() < today) {
            //过期记录，待处理
            throw new this.error.BusinessError('该记录已过期');
        }
        service.id = record.id;
        service.end_time = this.helper.YMDhms(new Date());
        service.status = this.constant.SVC_STATUS.EXCEPTION;
        await this.helper.makePromises()
            .push(dao.updateSvcRecord(service))
            .push(dao.updateAgedSvcStatus(service));
        const avgScore = await dao.getScoreByCompute(this.ctx.session.svc_staff_id);
        await dao.updateScore(this.ctx.session.svc_staff_id, avgScore)
    }

    async getMonthSvcRecords(year, month, pn, ps) {
        const dao = this.service.mobile.service.dao;
        const begin = this.helper.YMDhms(new Date(`${year}-${month}-01 00:00:00`));
        const end = this.helper.YMDhms(new Date(`${year}-${month + 1}-01 00:00:00`));
        const [records, count] = await this.helper.makePromises()
            .push(dao.getSvcRecordByDate(this.ctx.session.svc_staff_id, begin, end, pn, ps))
            .push(dao.getSvcCount(this.ctx.session.svc_staff_id))
            .execute();
        for (const r of records) {
            let start = new Date(r.start_time);
            r.date = this.helper.moment(start).format('MM/DD');
            r.time = this.helper.moment(start).format('HH:mm:ss');
            delete r.start_time;
        }
        return {
            count: count,
            list: records
        }
    }
}

module.exports = ServiceService;
